<?php
namespace control;
/*
*个人中心-个人档案类
*/
class myClass extends adIndexClass
{
    private const title = '个人档案类';
    public function home()
    {
        //所属部门
        $department = query("department", "id = '".$this->controlJob['departmentId']."'");
        //直属上级
        $manager = query("staff", " stid = '".$this->control['manager']."' ");
        $managerJob = query('job', " id = '$manager[jobId]'");
        $managerDepartment= query('department', " id = '$managerJob[departmentId]'");

        $talent = query('talent_pool', " id = '{$this->control['talentId']}'");
        //入职信息
        $staffEntryRecord = query('staff_entry_record', "talentId = '$talent[id]' and state = '已通过' order by time desc");
        $staffFormal = query('staff_formal', " staffId = '{$this->control['stid']}' and state = '已通过' order by time desc");
        $user = query('user', "stid = '{$this->control['stid']}'");
        $ico = [];
        $all = find(array('table' => 'file', 'where' => "targetId = '$talent[id]' and target in ('入职登记表','身份证正面','身份证背面','毕业证','学位证','银行卡','离职证明','体检报告','职称证书','竞业限制协议')"));
        foreach ($all['array'] as $array) {
            $ico[$array['target']] = ['id' => $array['id'], 'src' => $array['src']];
        }
        //身份证明
        $photo = $this->photo(array(
                array("title" => "头像","url" =>  $this->control['ico'] ?? ''),
                array("title" => "身份证正面","url" =>  $ico['身份证正面']['src'] ?? ''),
                array("title" => "身份证背面","url" =>  $ico['身份证背面']['src'] ?? ''),
                array("title" => "毕业证","url" => $ico['毕业证']['src'] ?? ''),
                array("title" => "学位证","url" => $ico['学位证']['src'] ?? ''),
                array("title" => "银行卡","url" => $ico['银行卡']['src'] ?? ''),
            ));
        //操作区
        $button[] = [
            "name" => "修改密码",
            "type" => "点击事件",
            "fun" => "layer('my','editPas',{})",
            'style' => 'li-btn-blue-bg',
        ];
        $html = $this->tableShow(array(
            "button" => $button,
            '基本信息' => array(
                array(
                    "员工姓名" => $this->control['name'],
                    "所属职位" => $department['name'].'-'.$this->controlJob['name'],
                    "联系方式" => $this->control['tel'],
                    "员工编号" => $this->control['orderNo'],
                ),
                array(
                    "身份证号" => $talent['idCard'],
                    "出生年月" => $talent['birthDay'],
                    "学历" =>  $talent['education'],
                    "专业" => $talent['major'],
                ),
                array(
                    "毕业院校" =>$talent['college'],
                    "参加工作时间" =>  self::null($talent['workDate']),
                    "婚姻状况" => $talent['marriage'],
                    "政治面貌" =>$talent['politicsStatus']
                ),
                array(
                    "户籍地址" =>  $talent['residenceAddress'],
                    "现居住地" => $talent['liveAddress'],
                ),
            ),
            '工作信息' => array(
                array(
                    "直属上级" => $managerDepartment['name'].'-'.$managerJob['name'].'-'.$manager['name'],
                    "入职日期" => self::null($staffEntryRecord['entryDate']),
                    "转正日期" => self::null($staffFormal['confirmDate']),
                    "工资卡银行名称" => $this->control['bankName'],
                ),
                array(
                    "工资卡账号" =>  $this->control['bankNum'],
                    "购买社保" =>$this->control['socialSecurity'],
                    "社保号" => $this->control['socialSecurityNum'],
                    "用户名" => $user['userName'],
                ),
            ),
            '身份证明'=>array(
                array(
                    $photo
                )
            )
        ));

        //基本资料
        $base =<<<eee
<div class="my-content p-10">
    <div class="right-content tab-content">{$html}</div>
</div>
eee;


        //JS

        //返回
        $html = $base;
        return $this->homeFun($html);
    }

    /*修改登录密码*/
    public function editPas()
    {
        //生成表单
        $html = $this->layerEdit(array("table" => array(
            array(
                must."原密码：" => password(array("name" => "oldPas")),
            ),
            array(
                must."新密码：" => password(array("name" => "newPas"))
            ),
            array(
                must."确认密码：" => password(array("name" => "surePas")),
            ),
        )));
        //返回
        return $this->layer(array("title" => "修改登录密码","height" => 240,"width" => 500,"html" => $html));
    }
    /*修改登录密码-发送手机短信验证码*/
    public function pasSms()
    {
        //赋值
        $session = getVal($_SESSION, 'pasSms');//防止重复发送短信的session
        $oldTime = (isset($session['time']) ? $session['time'] : 0) + 60;//最后一次发送验证码的时间不得晚于此时间
        //判断
        if ($oldTime > time()) {
            $json['warn'] = "发送验证码间隔不能低于一分钟";
        } else {
            $staff = query("staff", " stid = '".$this->control['stid']."' ");
            if (empty($staff['tel'])) {
                $json['warn'] = "此用户没有登记手机号码";
            } elseif (preg_match(checkTel, $staff['tel']) == 0) {
                $json['warn'] = "此用户手机号码格式有误";
            } else {
                //生成随机六位短信验证码
                $prove = rand(100000, 999999);
                //发送短信
                sms(array(
                    "tel" => $staff['tel'],
                    "TemplateCode" => para('aliyunTemplateCodeReset'),
                    "TemplateParam" => array("code" => $prove),
                ));
                //返回
                $_SESSION['pasSms'] = array("time" => time(),"prove" => $prove);
                $json['warn'] = "发送成功";
            }
        }
        return json_encode($json, 320);
    }
    /*我的桌面-修改登录密码*/
    public function editPasEdit()
    {
        $user = query("user"," stid = '".$this->control['stid']."' ");
        //赋值
        $newPasLength = mb_strlen($this->post('newPas'), 'utf-8');//新密码的长度
        $pattern = '/^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d]+$/';
        //判断
        if (empty($this->post('oldPas'))) {
            $json['warn'] = "请输入你的原密码";
        } elseif (verifyPwd($this->post('oldPas'), $user['password']) !== true) {
            $json['warn'] = "原密码错误";
        }elseif (empty($this->post('newPas'))) {
            $json['warn'] = "请输入你的新密码";
        }elseif (!preg_match($pattern, $this->post('newPas'))) {
            $json['warn'] = "密码必须是字母加数字组合";
        } elseif ($newPasLength < 6 or $newPasLength > 20) {
            $json['warn'] = "新密码长度必须大于6位且小于20位";
        } elseif (empty($this->post('surePas'))) {
            $json['warn'] = "请输入确认密码";
        } elseif ($this->post('newPas') != $this->post('surePas')) {
            $json['warn'] = "新密码与确认密码不一致";
        } else {
            $newPas = md5s($this->post('newPas'));
            $warn = update(array("table" => "user","field" => array("password" => $newPas,"updateTime" => date("Y-m-d H:i:s")),"where" => "stid = '".$this->control['stid']."'"));
            if ($warn == "更新成功") {
                $_SESSION['warn'] = $warn;
                $json['warn'] = 2;
                unset($_SESSION['pasSms']['prove']);
                //添加日志
                $text = $this->control['name']."修改了自己的登录密码";
                $this->logText(["type" => "my", "text" => $text]);
            } else {
                $json['warn'] = $warn;
            }
        }
        return json_encode($json, 320);
    }
}
